package com.leo.erp.web.materialproperty;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.leo.erp.dto.MaterialPropertyExecution;
import com.leo.erp.entity.MaterialProperty;
import com.leo.erp.enums.MaterialPropertyStateEnum;
import com.leo.erp.service.MaterialPropertyService;
import com.leo.erp.util.GlobalRuntimeException;
import com.leo.erp.util.HttpServletRequestUtil;

@Controller
@RequestMapping(value = "/materialProperty")
public class MaterialPropertyManagementController {
	@Autowired
	private MaterialPropertyService materialPropertyService;
	
	@RequestMapping(value = "/list")
	@ResponseBody
	public Map<String, Object> list(HttpServletRequest request){
		Map<String, Object> modelMap = new HashMap<>();
		//获取前台传过来的值
		String nativeName = HttpServletRequestUtil.getString(request, "name");
		int currentPage = HttpServletRequestUtil.getInt(request, "currentPage");
		int pageSize = HttpServletRequestUtil.getInt(request, "pageSize");
		
		MaterialPropertyExecution me = materialPropertyService.getPageList(nativeName, currentPage, pageSize);
		if(me.getState() == MaterialPropertyStateEnum.SUCCESS.getState()) {
			modelMap.put("success", true);
			modelMap.put("pages",me.getPages());//总页数
			modelMap.put("total", me.getTotal());//总数
			
			Map<String, Object> map = new HashMap<>();
			map.put("rows", me.getMaterialPropertyPageList());
			map.put("total", me.getTotal());
			modelMap.put("materialPropertyList", map);
		}else {
			modelMap.put("success", false);
			modelMap.put("msg", "查询列表失败");
		}
		
		return modelMap;
	}
	
	@RequestMapping(value = "/updateMaterialProperty")
	@ResponseBody
	public Map<String, Object> updateMaterialProperty(HttpServletRequest request){
		Map<String, Object> modelMap = new HashMap<>();
		//获取前台传过来的值
		String beanJson = HttpServletRequestUtil.getString(request, "info");
		int materialPropertyId = HttpServletRequestUtil.getInt(request, "id");
		String nativeName = HttpServletRequestUtil.getString(request, "nativeName");
		
		try {
			MaterialProperty materialProperty = JSON.parseObject(beanJson, MaterialProperty.class);
			materialProperty.setMaterialPropertyId(materialPropertyId);
			materialProperty.setNativeName(nativeName);
			
			//更新
			MaterialPropertyExecution me = materialPropertyService.updatematerialProperty(materialProperty);
			if(me.getState() == MaterialPropertyStateEnum.SUCCESS.getState()) {
				modelMap.put("success", true);
			}else {
				modelMap.put("success", false);
				modelMap.put("msg", "更新数据异常");
			}
			
		} catch (GlobalRuntimeException e) {
			e.printStackTrace();
		}
		
		return modelMap;
	}
}
